﻿/*
* jquery-datetimepicker v1.0.0
*
* @requires jQuery v1.4.4 or later.
* @requries jQuery UI v1.8.10 or later.
*
* Copyright (c) 2011 Cybozu, Inc.
*   http://labs.cybozu.co.jp/
*
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*/
(function ($) {

    $.fn.datetimepicker = function (options) {

        if (datetimepickerMethods[options]) {
            return datetimepickerMethods[options].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (options && typeof options != "object") {
            $.error("Method " + options + " does not exists on datetimepicker.");
        }

        var settings = { hoursMode24: false, minutesStep: 5, required: false };
        settings = $.extend(settings, options);

        this.each(function () {

            var $this = $(this);

            // datepicker
            var $datepicker = $('<input type="text" class="dtp-date" />').appendTo($this);
            $datepicker.datepicker(options);

            // hours select
            var $hoursSelect = $('<select class="dtp-hours"></select>').appendTo($this);
            if (!settings.required) {
                appendOption($hoursSelect, "", "");
            }
            for (var i = 0; i < 24; i++) {
                if (settings.hoursMode24) {
                    appendOption($hoursSelect, i, nn(i) + ":");
                } else {
                    var hours = i % 12;
                    if (hours == 0) hours = 12;
                    var text = hours + ((i < 12) ? " AM" : " PM");
                    appendOption($hoursSelect, i, text);
                }
            }

            // minutes select
            var $minutesSelect = $('<select class="dtp-minutes"></select>').appendTo($this);
            if (!settings.required) {
                appendOption($minutesSelect, "", "");
            }
            for (var i = 0; i < 60; i += settings.minutesStep) {
                appendOption($minutesSelect, i, nn(i));
            }
        });

        return this;
    };

    var datetimepickerMethods = {
        destroy: function () {
            $(".dtp-date", this).datepicker("destory");
            this.empty();
            return this;
        },

        disable: function () {
            $(".dtp-date", this).datepicker("disable");
            $("select", this).attr("disabled", "disabled");
            return this;
        },

        enable: function () {
            $(".dtp-date", this).datepicker("enable");
            $("select", this).attr("disabled", "");
            return this;
        },

        option: function (arg1, arg2) {
            if (arg2 == null || arg2 == undefined) {
                if ($.isPlainObject(arg1)) {
                    $(".dtp-date", this).datepicker("option", arg1);
                } else {
                    return $(".dtp-date", this).datepicker("option", arg1);
                }
            } else {
                $(".dtp-date", this).datepicker("option", arg1, arg2);
            }
            return this;
        },

        widget: function () {
            return $(".dtp-date", this).datepicker("widget");
        },

        isDisabled: function () {
            return $(".dtp-date", this).datepicker("isDisabled");
        },

        hide: function () {
            return $(".dtp-date", this).datepicker("hide");
            return this;
        },

        show: function () {
            return $(".dtp-date", this).datepicker("show");
            return this;
        },

        refresh: function () {
            return $(".dtp-date", this).datepicker("refresh");
            return this;
        },

        getDate: function () {
            var date = $(".dtp-date:first", this).datepicker("getDate");
            if (!date) return null;
            var hours = $("select.dtp-hours:first", this).val();
            hours = (hours == "") ? 0 : parseInt(hours, 10);
            var minutes = $("select.dtp-minutes:first", this).val();
            minutes = (minutes == "") ? 0 : parseInt(minutes, 10);
            return new Date(date.getFullYear(), date.getMonth(), date.getDate(), hours, minutes, 0);
        },

        setDate: function (date) {
            $(".dtp-date", this).datepicker("setDate", date);
            $("select.dtp-hours", this).val(date.getHours());
            $("select.dtp-minutes", this).val(date.getMinutes());
            return this;
        },

        isDateOnly: function () {
            var date = $(".dtp-date:first", this).datepicker("getDate");
            if (!date) return false;
            var hours = $("select.dtp-hours:first", this).val();
            return hours == "";
        }
    };

    // private functions

    function appendOption($select, value, text) {
        $select.append('<option value="' + value + '">' + text + '</option>');
    }

    function nn(num) {
        return ((num < 10) ? "0" : "") + num;
    }

})(jQuery);
